auto.waitFor();

var myFunc = require("./myCommonFunction.js");
var ret = myFunc.initNetWork(null, true);
var widthG = device.width;
var heightG = device.height

// threads.start(function () {
//     sleep(300000)
//     exit();
// });
threads.start(function () {
    if (ret) {
        opreate();
    }
    exit();
});

function opreate() {
    try {
        myFunc.openMyApp("菜鸟");
        myFunc.clickByText('包裹星球', 5000, 1)
        myFunc.findOne('金包裹', 15000)
        if (myFunc.clickByTextC('去完成', 2000, 1)) {
            sleep(2000);
            scrollDown();
            sleep(105000);
            app.launchApp('菜鸟')
            sleep(1000);
            myFunc.tuichuToStr('首页')
            qiurenwu();
        }
        qiurenwu();
        renwu()
        myFunc.tuichu("菜鸟");
    } catch (error) {
        log(error);
        myFunc.tuichu("菜鸟");
        opreate();
    }
}

function qiurenwu() {
    myFunc.clickByText('包裹星球', 5000, 1)
    myFunc.findOne('金包裹', 15000)
    sleep(3000 + random(500, 1000));
    lingjian();
    if (myFunc.findOne('后消失', 1000) != null) {
        myFunc.log('后消失')
        click('后消失')
        sleep(1500 + random(500, 1000));
        myFunc.clickByTextC('去完成', 2000, 1)
        sleep(3000 + random(500, 1000));
        scrollDown();
        sleep(102000 + random(1500, 2000));
        app.launchApp('菜鸟')
        sleep(1000);
        myFunc.tuichuToStr('首页')
        qiurenwu();
    }

}

function renwu() {
    if(myFunc.findOnce('今日活跃度') == null && !myFunc.clickByText('百万星球宝藏等你来赢取', 5000, 1)){
        myFunc.tuichu("菜鸟");
        opreate();
        return
    }
    sleep(2000);
    var a = myFunc.find('去完成');
    if (a.length < 2) {
        if(myFunc.findOnce('今日活跃度') == null) {
            myFunc.tuichu("菜鸟");
            opreate();
        }
        return
    }
    lo1: for (let index = 0; index < a.length; index++) {
        let button = a[index];
        var c = button.bounds();
        var b = className('android.view.View').boundsInside(200, c.top, c.left, c.bottom).indexInParent(0).depth(20).find()
        for (let idx = 0; idx < b.length; idx++) {
            let element = b[idx];
            var taskText = element.text();
            myFunc.log(taskText)
            if (taskText == '逛天天领红包40秒') {
                for (let index = 0; index < 30; index++) {
                    myFunc.log('领红包浏览40s')
                    myFunc.clickCenter(button, 1);
                    sleep(3000 + random(500, 1000));
                    if(myFunc.findOnce('天天领红包') != null) {
                        scrollDown();
                        sleep(40000 + random(1500, 2000));
                    }
                    app.launchApp('菜鸟')
                    sleep(1000);
                    if (!myFunc.clickByTextC('去领取', 1000, 1)) {
                        myFunc.tuichuToStr('逛天天领红包40秒')
                    }
                    sleep(2000 + random(500, 1000));

                    lingjian();
                    var q = myFunc.findOnce('逛天天领红包40秒');
                    if (q == null || heightG - q.bounds().bottom < 20) {
                        myFunc.log('结束')
                        break
                    }
                }
                break lo1;
            } else if (taskText.includes('逛主会场商品')) {
                逛主会场(button);
                break lo1;
            } else if (taskText.includes('去裹酱积分夺宝')
                || taskText.includes('逛逛菜鸟绿色家园')
                || taskText.includes('去爱心包裹做公益')
                || taskText.includes('领初秋')
                || taskText.includes('去逛逛集卡赢免单')
            ) {
                look(taskText, button)
                break lo1;
            } else if (taskText.includes('去免费领水果浇1次水')) {
                myFunc.clickCenter(button, 1);
                sleep(2000 + random(500, 1000));
                myFunc.clickByText('开心收下', 5000, 1)
                sleep(2000 + random(500, 1000));
                click(143 + random(-10, 10), 1847 + random(-10, 10));
                sleep(1000 + random(500, 1000));
                myFunc.clickByText('去领取', 2000, 1)
                myFunc.clickByText('领取', 2000, 1)
                sleep(500 + random(500, 1000));
                click(500 + random(-100, 100), 300 + random(-50, 100));
                sleep(500 + random(500, 1000));
                click(914 + random(-10, 10), 1847 + random(-10, 10));
                click(914 + random(-10, 10), 1847 + random(-10, 10));
                myFunc.tuichuToStr('今日活跃度')
                sleep(2000 + random(500, 1000));
                lingjian();
                break lo1;
            } else if (taskText.includes('30s') || taskText.includes('30秒')) {
                myFunc.clickCenter(button, 1);
                if (taskText.indexOf('搜索心仪好物') != -1) {
                    sleep(1000 + random(1500, 2000));
                    myFunc.clickByText('搜索', 5000, true);
                }
                sleep(3000 + random(500, 1000));
                scrollDown();
                sleep(32000 + random(1500, 2000));
                app.launchApp('菜鸟')
                sleep(1000);
                myFunc.tuichuToStr('今日活跃度')
                sleep(2000 + random(500, 1000));
                lingjian();
                break lo1;
            }
        }
        sleep(2000);
    }

    if(myFunc.findOnce('今日活跃度') != null && myFunc.find('去完成').length > 1){
        click(1020 + random(5, 10), 720 +  + random(5, 10))
        sleep(1000);
        renwu()
    }
}

function look(taskText, button) {
    myFunc.clickCenter(button, 1);
    sleep(5000 + random(500, 1000));
    myFunc.tuichuToStr('今日活跃度')
    sleep(2000 + random(500, 1000));
    lingjian();
}

function 逛主会场(button) {
    var a = myFunc.findOnce('逛主会场商品', true)
    if (a != null && heightG - a.bounds().bottom > 10 && a.bounds().bottom < heightG) {
        myFunc.log(a.text())
        myFunc.clickCenter(button, 1);
        sleep(3000 + random(500, 1500));
        scrollDown();
        if (myFunc.findOne('滑动', 3000, true) == null) {
            log('点错了')
            while (scrollUp()) { }
            myFunc.clickByText('百万星球宝藏等你来赢取', 5000, 1)
            sleep(2000);
            逛主会场()
            return;
        }
        sleep(a.text().match(/\d+/)[0] * 1000 + random(1500, 2000));
        app.launchApp('菜鸟')
        sleep(1000);
        if (!myFunc.clickByTextC('去领取', 1000, 1)) {
            myFunc.tuichuToStr('今日活跃度')
        }
        sleep(2000 + random(500, 1000));
        lingjian();
        sleep(2000);
        逛主会场(button);
    }
}

function lingjian() {
    var jianli = text('领取奖励').find();
    if (jianli.length > 0) {
        var jian = jianli[0];
        myFunc.log('领取奖励')
        myFunc.clickCenter(jian, 1);
        sleep(5000 + random(500, 1000));
        yincang();
    }
    if (jianli.length > 1) {
        lingjian()
    }
}
function yincang() {
    if (myFunc.findOnce('关闭将失去该隐藏任务') != null) {
        myFunc.log('隐藏任务')
        myFunc.clickByTextC('去完成', 2000, 1)
        sleep(3000 + random(500, 1000));
        scrollDown();
        sleep(47000 + + random(1500, 2000));
        app.launchApp('菜鸟')
        sleep(1000);
        if (!myFunc.clickByTextC('去领取', 1000, 1)) {
            myFunc.tuichuToStr('今日活跃度')
        }
        sleep(2000 + random(500, 1000));
        lingjian()
    }
}
